package com.ruoyi.project.system.exam.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.system.exam.mapper.PkBattleMapper;
import com.ruoyi.project.system.exam.domain.PkBattle;
import com.ruoyi.project.system.exam.service.IPkBattleService;
import com.ruoyi.common.utils.text.Convert;

/**
 * PK对战服务实现类
 * 
 * @author ruoyi
 * @date 2023-07-16
 */
@Service
public class PkBattleServiceImpl implements IPkBattleService 
{
    @Autowired
    private PkBattleMapper pkBattleMapper;

    /**
     * 查询PK对战
     * 
     * @param id PK对战主键
     * @return PK对战
     */
    @Override
    public PkBattle selectPkBattleById(Long id)
    {
        return pkBattleMapper.selectPkBattleById(id);
    }

    /**
     * 查询PK对战列表
     * 
     * @param pkBattle PK对战
     * @return PK对战
     */
    @Override
    public List<PkBattle> selectPkBattleList(PkBattle pkBattle)
    {
        List<PkBattle> list = pkBattleMapper.selectPkBattleList(pkBattle);
        
        // 过滤掉双方都已完成答题的对战记录（即状态为已完成的对战）
        list.removeIf(battle -> 
            battle.getBattleStatus() != null && battle.getBattleStatus() == 3 &&
            battle.getInitiatorAnswered() != null && battle.getInitiatorAnswered() == 1 &&
            battle.getChallengerAnswered() != null && battle.getChallengerAnswered() == 1
        );
        
        return list;
    }

    /**
     * 根据用户ID查询PK对战列表
     * 
     * @param userId 用户ID
     * @return PK对战集合
     */
    @Override
    public List<PkBattle> selectPkBattleByUserId(Long userId)
    {
        return pkBattleMapper.selectPkBattleByUserId(userId);
    }

    /**
     * 新增PK对战
     * 
     * @param pkBattle PK对战
     * @return 结果
     */
    @Override
    public int insertPkBattle(PkBattle pkBattle)
    {
        pkBattle.setCreateTime(DateUtils.getNowDate());
        return pkBattleMapper.insertPkBattle(pkBattle);
    }

    /**
     * 修改PK对战
     * 
     * @param pkBattle PK对战
     * @return 结果
     */
    @Override
    public int updatePkBattle(PkBattle pkBattle)
    {
        pkBattle.setUpdateTime(DateUtils.getNowDate());
        return pkBattleMapper.updatePkBattle(pkBattle);
    }

    /**
     * 批量删除PK对战
     * 
     * @param ids 需要删除的PK对战主键
     * @return 结果
     */
    @Override
    public int deletePkBattleByIds(Long[] ids)
    {
        return pkBattleMapper.deletePkBattleByIds(ids);
    }

    /**
     * 删除PK对战信息
     * 
     * @param id PK对战主键
     * @return 结果
     */
    @Override
    public int deletePkBattleById(Long id)
    {
        return pkBattleMapper.deletePkBattleById(id);
    }

    /**
     * 查询已完成的PK对战列表
     * 
     * @param pkBattle PK对战查询条件
     * @return 已完成的PK对战列表
     */
    @Override
    public List<PkBattle> selectFinishedPkBattleList(PkBattle pkBattle) {
        List<PkBattle> list = pkBattleMapper.selectPkBattleList(pkBattle);
        
        // 过滤并返回双方都已完成答题的对战记录（状态为3，且双方answered字段均为1）
        return list.stream()
            .filter(battle -> battle.getBattleStatus() != null && battle.getBattleStatus() == 3)
            .filter(battle -> battle.getInitiatorAnswered() != null && battle.getInitiatorAnswered() == 1)
            .filter(battle -> battle.getChallengerAnswered() != null && battle.getChallengerAnswered() == 1)
            .collect(Collectors.toList());
    }
}